//
//  HJHomeViewController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/19.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJHomeViewController.h"
#import "HJHomeCell.h"
#import "HJHomeCollectionCell.h"
#import "HJHomeDetailController.h"
#import "HJSearchResultsController.h"
#import "HJHomeModel.h"
#import "HJSearchNavBar.h"
#import "HJSearchController.h"

static NSString * CellIndentifier = @"CELL";

@interface HJHomeViewController ()<SDCycleScrollViewDelegate,searchBarDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic,strong) NSMutableArray * hostryArr;
@property (nonatomic, strong) SDCycleScrollView *cycleBanner;
@property(nonatomic,strong)NSMutableArray <HJHomeModel *>*shoesArr;
@property(nonatomic,strong)NSMutableArray <HJHomeModel *>*shoesHotArr;
@property(nonatomic,strong)HJSearchNavBar * navBar;
@end

@implementation HJHomeViewController
-(NSMutableArray<HJHomeModel *> *)shoesArr {
    if (!_shoesArr) {
        _shoesArr = [NSMutableArray array];
    }
    return _shoesArr;
}

-(NSMutableArray<HJHomeModel *> *)shoesHotArr {
    if (!_shoesHotArr) {
        _shoesHotArr = [NSMutableArray array];
    }
    return _shoesHotArr;
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    
}
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNaviTitleView];
    self.hostryArr = [NSMutableArray array];
    self.automaticallyAdjustsScrollViewInsets =YES;
}

-(void)configTableView {
    self.needRefresh = YES;
    self.tableViewStyle = UITableViewStyleGrouped;
    [super configTableView];
    CGFloat H;
    if (mScreenHeight<812) {
        H = mScreenHeight-94-10;
    }else {
        H = mScreenHeight-94-44;
    }
    self.tableView.frame = CGRectMake(0, 94, mScreenWidth, H);
    self.tableView.tableHeaderView = [self configTableHeaderView];
    [self.tableView registerCellByNibName:@"HJHomeCell"];
}

-(void)configNaviTitleView {
    self.navBar = [[HJSearchNavBar alloc]init];
    self.navBar.frame = CGRectMake(0, 0, mScreenWidth, 94);
    self.navBar.btnW.constant = 0.01;
    self.navBar.textW.constant = 0.01;
    self.navBar.searchBtn.hidden = NO;
    self.navBar.delegate = self;
    [self.view addSubview:self.navBar];
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.navBar.frame = CGRectMake(0, 0, mScreenWidth, 94);
}
-(void)searchBtnClicked {
    HJSearchController * searchResultsVC = [[HJSearchController alloc]init];
    [self.navigationController pushViewController:searchResultsVC animated:YES];
}

-(UIView *)configTableHeaderView {
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, mScreenWidth, 190);
    
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.frame = CGRectMake(0, 0, mScreenWidth, view.height);
    _scrollView.contentSize = CGSizeMake(0, view.height);
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [view addSubview:_scrollView];
    
    if (!_cycleBanner) {
        _cycleBanner = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, mScreenWidth, _scrollView.height) imageURLStringsGroup:nil];
        _cycleBanner.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _cycleBanner.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
        _cycleBanner.delegate = self;
        _cycleBanner.backgroundColor = [UIColor clearColor];
        _cycleBanner.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        _cycleBanner.clipsToBounds = YES;
        [self loadNetworkData];
        [_scrollView addSubview:_cycleBanner];
    }
    
    return view;
}

-(void)loadNetworkData {
    [TZHttpTool post:HomeAPI params:@{@"page":@(self.page)} success:^(NSDictionary *result) {
        NSArray * arr = result[@"data"][@"ads"];
        NSMutableArray *imgArr =[NSMutableArray array];
        for (NSDictionary *dic in arr) {
            NSString * imgUrl = [NSString stringWithFormat:@"%@/%@",dic[@"img_base_url"],dic[@"img_path"]];
            [imgArr addObject:imgUrl];
        }
        _cycleBanner.imageURLStringsGroup = imgArr;
        NSArray *shoesArr = [HJHomeModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"shoes"]];
        NSArray *shoesHotArr = [HJHomeModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"shoes_hot"][@"data"]];
        if (self.page==1) {
            [self.shoesArr removeAllObjects];
            [self.shoesHotArr removeAllObjects];
        }
        [self.shoesArr addObjectsFromArray:shoesArr];
        [self.shoesHotArr addObjectsFromArray:shoesHotArr];
        self.totalPage = result[@"data"][@"shoes_hot"][@"totalPage"];
        [self.tableView reloadData];
        [self.tableView endRefresh];
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return 1;
    }
    return self.shoesHotArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
        HJHomeCollectionCell *collectionCell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
        collectionCell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (!collectionCell) {
            collectionCell = [[HJHomeCollectionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier];
        }
        collectionCell.modelArr = [NSMutableArray arrayWithArray:self.shoesArr];
        MJWeakSelf;
        collectionCell.block = ^{
            HJHomeModel * model = self.shoesArr[indexPath.row];
            [weakSelf pushDetailController:model.shoes_id];
        };
        return collectionCell;
    }else {
        HJHomeCell *homeCell = [tableView dequeueReusableCellWithIdentifier:@"HJHomeCell"];
        homeCell.selectionStyle = UITableViewCellSelectionStyleNone;
        homeCell.model = self.shoesHotArr[indexPath.row];
        return homeCell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 165;
    }
    return 130;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [self configHeaderSection:@[@"有货商品",@"热销商品"] imageArr:@[@"Goodsinstock",@"hotcakes"] section:section];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section ==1) {
        HJHomeModel * model = self.shoesHotArr[indexPath.row];
        [self pushDetailController:model.shoes_id];
    }
}

#pragma mark -- 跳转详情
-(void)pushDetailController:(NSString *)shoes_id {
    HJHomeDetailController *homeDetailVC = [[HJHomeDetailController alloc]init];
    homeDetailVC.shoes_id = shoes_id;
    [self.navigationController pushViewController:homeDetailVC animated:YES];
}

#pragma mark -- section
-(UIView *)configHeaderSection:(NSArray*)titleArr imageArr:(NSArray*)imageArr section:(NSInteger)section {
    UIView *sectionView = [[UIView alloc]init];
    sectionView.frame = CGRectMake(0, 0, mScreenWidth, 60);
    sectionView.backgroundColor = [UIColor whiteColor];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(16, sectionView.centerY, 90, 22);
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    [btn setTitle:titleArr[section] forState:UIControlStateNormal];
    [btn setImage:HJImage(imageArr[section]) forState:UIControlStateNormal];
    btn.userInteractionEnabled = NO;
    btn.titleLabel.font = HJFontSize(16);
    [btn setTitleColor:HJColorRGB(0) forState:UIControlStateNormal];
    [sectionView addSubview:btn];

    return sectionView;
}
@end
